#include "xxt_bus_data.hpp"
#include <any>
#include <string>
#include <tuple>
#include "content/data_provider.hpp"
#include "content/image.hpp"
#include "core/assert.hpp"
#include "core/log.hpp"
#include "utils/safe_value.hpp"

std::optional<std::tuple<value_type, std::any>> XxtBusData::get_data (const std::string &key)
{
    if (key == "cities")
    {
        enumerate_t ret{
                .count = &count,
                .text = +[](void *p, u32 i) -> const char* {
                    decltype(infos) *list = (decltype(infos) *) p;
                    return (*list)[i].name.c_str ();
                },
                .user_data = &infos,
        };
        return std::make_tuple (value_type::ENUMERATE, std::any{ret});
    }
    if (key == "hello_cmd")
    {
        callback_t                  ret{
                .action = +[](void *p) {
                    auto This = (XxtBusData *) p;
                    This->Hello_CMD ();
                },
                .user_data = this,
        };
        return std::make_tuple (value_type::CALLBACK, std::any{ret});
    }
    if (key == "switch_value")
    {
        return std::make_tuple (value_type::BOOL, std::any{&on_off});
    }
    if (key == "slider_value")
    {
        return std::make_tuple (value_type::S32, std::any{&slider_val});
    }
    if (key == "tooltip")
    {
        return std::make_tuple (value_type::STD_STRING, std::any{&tooltip});
    }
    if (key == "icon")
    {
        return std::make_tuple (value_type::IMAGE, std::any{&icon});
    }
    if (key == "selected")
    {
        return std::make_tuple (value_type::S32, std::any{&selected});
    }
    return {};
}

XxtBusData::XxtBusData ()
{
    count.value = 5;
    PI_ASSERT(count.get_s64() == 5);
    for (int i = 0; i < 5; ++i)
    {
        infos.emplace_back (std::string{"City #"} + std::to_string (i + 1), i, i, i);
    }
    selected = 0;
    tooltip  = "测试😚文本emoji🔣";
    
    for(int i=1; i<=4; i++)
    {
        RESOURCE_IMAGE(images[i-1], "/test_images/img"+std::to_string(i)+".jpg");
    }
    icon = images[0];
}

void XxtBusData::Hello_CMD ()
{
    static int icon_idx = 0;
    L_INFO ("You found me!!");
    icon = images[++icon_idx & 3];
}
